<template>
  <div>
    <el-breadcrumb separator="/" style="margin-top: 20px">
      <el-breadcrumb-item :to="{ path: '/manager/order/tackOut' }"
        >首页</el-breadcrumb-item
      >
      <el-breadcrumb-item :to="{ path: '/manager/order/viewInformation' }"
        >查看订单信息</el-breadcrumb-item
      >
    </el-breadcrumb>

    <el-dialog v-model="dialogFormVisible" title="评价订单" width="500">
      <el-form :model="form">
        <el-form-item label="评级等级" :label-width="formLabelWidth">
          <el-rate v-model="form.grade" />
        </el-form-item>
        <el-form-item label="评级内容" :label-width="formLabelWidth">
          <el-input v-model="form.content" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消评价</el-button>
          <el-button type="primary" @click="evaluateCreate"> 确认评价 </el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog v-model="ViewDialogFormVisible" title="评价信息" width="500">
      <el-card style="max-width: 480px">
        <p>评价内容：{{ infor.content }}</p>
        <p>评级等级：<el-rate v-model="infor.grade" disabled /></p>
        <p>评价时间：{{ infor.inputTime }}</p>
        <p>
          评价回复：{{ infor.feedbackContent == null ? "暂无" : infor.feedbackContent }}
        </p>
      </el-card>
    </el-dialog>

    <el-dialog title="订单详情" v-model="orderDetailVisible">
      <el-table :data="orderDetailInfo.menuCounts" border height="350">
        <el-table-column label="菜品名称" prop="menuName"></el-table-column>
        <el-table-column label="价格">
          <template #default="scope">
            {{ "￥" + scope.row.menuPrice }}
          </template>
        </el-table-column>
        <el-table-column label="数量" prop="count"></el-table-column>
        <el-table-column label="图片">
          <template #default="scope">
            <el-image
              style="width: 50px; height: 50px; border-radius: 5px"
              :src="scope.row.menuPicture"
              fit="cover"
              ><template #error>
                <div class="image-slot">
                  <el-icon><icon-picture /></el-icon>
                </div> </template
            ></el-image>
          </template>
        </el-table-column>
      </el-table>
      <el-form>
        <el-form-item label="配送状态" v-show="orderDetailInfo.type == 2">
          <span style="font-weight: bolder">
            {{
              orderDetailInfo.sendState ? orderDetailInfo.sendState : "待骑手接单"
            }}</span
          >
        </el-form-item>
        <el-form-item label="配送员" v-show="orderDetailInfo.senderName">{{
          orderDetailInfo.senderName
        }}</el-form-item>
        <el-form-item label="订单状态">{{
          orderDetailInfo.applyState == -1
            ? "已退款"
            : orderDetailInfo.applyState == 0
            ? "待支付"
            : orderDetailInfo.applyState == 1
            ? "已支付"
            : orderDetailInfo.applyState == 2
            ? "已评价"
            : "未知"
        }}</el-form-item>
        <el-form-item label="金额">{{ "￥" + orderDetailInfo.money }}</el-form-item>
        <el-form-item label="备注">{{
          orderDetailInfo.demo ? orderDetailInfo.demo : "暂无"
        }}</el-form-item>
        <el-form-item>
          <el-button
            type="warning"
            v-show="orderDetailInfo.applyState == 1"
            @click="evaluate(orderDetailInfo.orderId)"
            >确认并评价</el-button
          >
          <el-button
            type="primary"
            v-show="orderDetailInfo.applyState == 0"
            @click="applyOrder(orderDetailInfo.orderId)"
            >支付</el-button
          >
          <el-button
            type="primary"
            v-show="orderDetailInfo.applyState == 1"
            @click="backMoney(orderDetailInfo.orderId)"
            >退款</el-button
          >
          <el-button
            key="plain"
            type="info"
            v-show="orderDetailInfo.applyState == 2"
            @click="ViewEvaluate(orderDetailInfo.orderId)"
            >评价信息</el-button
          >
          <el-button type="primary" @click="linkShop(orderDetailInfo.orderId)"
            >联系商家</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-table :data="tableData" height="75vh" style="width: 100%">
      <el-table-column label="商店名称" width="180">
        <template #default="scope">
          {{ scope.row.shopName }}
        </template>
      </el-table-column>
      <el-table-column label="金额" width="100">
        <template #default="scope">
          {{ "￥" + scope.row.money }}
        </template>
      </el-table-column>

      <el-table-column label="点餐类型" width="100">
        <template #default="scope">
          {{ scope.row.type == 1 ? "堂食" : "外卖" }}
        </template>
      </el-table-column>
      <el-table-column prop="inputDate" label="下单时间" width="200" />
      <el-table-column prop="address" label="配送地址" />

      <el-table-column prop="demo" label="备注" />

      <el-table-column label="订单状态" width="100">
        <template #default="scope">
          {{
            scope.row.applyState == 0
              ? "待支付"
              : scope.row.applyState == 1
              ? "已支付"
              : scope.row.applyState == -1
              ? "已退款"
              : scope.row.applyState == 2
              ? "已评价"
              : "未知"
          }}
        </template>
      </el-table-column>

      <el-table-column width="250">
        <template #default="scope">
          <el-button type="primary" @click="ViewDetail(scope.row.orderId)"
            >查看详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      v-model="ChatWithShop"
      title="聊天对话框"
      width="500"
      :before-close="handleClose"
    >
      <div class="chat-container" style="height: 300px; width: 100%; overflow-y: auto">
        <div class="chat-container">
          <div
            v-for="(message, index) in ChatList"
            :key="index"
            :class="getMessageClass(message)"
          >
            {{ message.content }}
          </div>
        </div>
      </div>
      <div>
        <el-input v-model="content" style="width: 400px" placeholder="请输入聊天内容" />
        <el-button type="primary" @click="SenderOnline">发送</el-button>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="ChatWithShop = false">Cancel</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import axios from "axios";
import Vue, { ref, reactive } from "vue";
import { ElMessage } from "element-plus";
import router from "@/router";

const token = localStorage.getItem("student_auth_token");
const tableData = ref([]);
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";
const orderDetailInfo = ref([]);
const orderId = router.currentRoute.value.query.id;
const ViewDialogFormVisible = ref(false);
const form = reactive({
  orderId: 0,
  grade: 5,
  content: "",
});
const ChatWithShop = ref(false);
const orderDetailVisible = ref(false);
const ViewDetail = (id) => {
  axios
    .get("/api/order/view/" + id, {
      headers: {
        token: token,
      },
    })
    .then((res) => {
      // console.log("code line-120 \n\r😂 res:\n\r", res);
      if (res.data.success) {
        orderDetailInfo.value = res.data.message;
      } else {
        ElMessage({
          type: "error",
          message: "获取订单信息失败",
        });
      }
    })
    .catch((err) => {});
  orderDetailVisible.value = true;
};

const applyOrder = (id) => {
  axios
    .get("/api/order/apply/" + id, {
      headers: {
        token: token,
      },
    })
    .then((res) => {
      // console.log("code line-193 \n\r😌 res:\n\r", res);
      if (res.data.success) {
        ElMessage({
          type: "success",
          message: "支付成功",
        });
      } else {
        ElMessage({
          type: "error",
          message: res.data.message,
        });
      }
      getData();
      orderDetailVisible.value = false;
    })
    .catch((err) => {});
};

const ViewEvaluate = (index: number) => {
  axios
    .get("/api/evaluate/select/" + index, {
      headers: {
        token: token,
      },
    })
    .then((res) => {
      // console.log(res);
      // console.log("code line-104 \n\r😍 res:\n\r", res);
      const msg = res.data.message;
      infor.content = msg.content;
      infor.evaluateId = msg.evaluateId;
      infor.grade = msg.grade;
      infor.inputTime = msg.inputTime;
      infor.feedbackContent = msg.feedbackContent;
    })
    .catch((err) => {
      console.log(err);
    });
  ViewDialogFormVisible.value = true;
};

const backMoney = (id) => {
  axios
    .get("/api/order/refund/" + id, {
      headers: {
        token: token,
      },
    })
    .then((res) => {
      // console.log("code line-255 \n\r😀 res:\n\r", res);
      if (res.data.success) {
        ElMessage({
          type: "success",
          message: "退款成功",
        });
      } else {
        ElMessage({
          type: "error",
          message: res.data.message,
        });
      }
    })
    .catch((err) => {});
  getData();
  orderDetailVisible.value = false;
};
const ChatList = ref([]);
const content = ref("");
const order = ref("");
const linkShop = (orderId) => {
  order.value = orderId;
  axios
    .get("/api/online/all/chat/shop/" + orderId, {
      headers: {
        token: token,
      },
    })
    .then((res) => {
      console.log("code line-296 \n\r😇 result:\n\r", res);
      if (res.data.success) {
        ChatWithShop.value = true;
        ChatList.value = res.data.message;
      } else {
        ElMessage({
          type: "error",
          message: res.data.message,
        });
      }
    })
    .catch((err) => {});
};
const SenderOnline = () => {
  axios
    .get("/api/online/chat/shop/" + order.value + "?content=" + content.value, {
      headers: {
        token: token,
      },
    })
    .then((res) => {
      console.log("code line-343 \n\r😃 res:\n\r", res);
      if (res.data.success) {
        ElMessage({
          type: "success",
          message: "发送成功",
        });
        content.value = "";
      } else {
        ElMessage({
          type: "error",
          message: res.data.message,
        });
      }
      linkShop(order.value);
    })
    .catch((err) => {});
};

const infor = reactive({
  content: "",
  evaluateId: "",
  grade: "",
  inputTime: "",
  feedbackContent: "",
});
const evaluate = (id) => {
  form.orderId = id;
  form.content = "";
  form.grade = 5;
  dialogFormVisible.value = true;
};
const evaluateCreate = () => {
  const req = {
    orderId: form.orderId,
    grade: form.grade,
    content: form.content,
  };
  axios
    .post("/api/evaluate/create", req, {
      headers: {
        token: token,
      },
    })
    .then((res) => {
      // console.log(res);
      if (res.data.success) {
        ElMessage({
          message: "评价成功",
          type: "success",
        });
      } else {
        ElMessage({
          message: res.data.message,
          type: "error",
        });
      }
    })
    .catch((err) => {
      console.log(err);
    });

  dialogFormVisible.value = false;
  getData();
  orderDetailVisible.value = false;
};
const getData = () => {
  axios
    .get("/api/order/query", {
      headers: {
        token: token,
      },
    })
    .then((res) => {
      // console.log(res);
      const msg = res.data.message;
      for (let index = 0; index < msg.length; index++) {
        const el = msg[index];
        el.senderName = JSON.parse(el.senderName);
      }
      tableData.value = msg;
    })
    .catch((err) => {
      console.log(err);
    });
};
getData();
const getMessageClass = (message) => {
  // 根据发送人和接收人的ID来确定消息的样式
  if (message.sendId === "当前用户的ID") {
    return "sent-message"; // 发送方样式
  } else {
    return "received-message"; // 接收方样式
  }
};
</script>

<style lang="scss" scoped>
.sent-message {
  text-align: right; /* 发送方消息右对齐 */
  /* 其他样式设置 */
}

.received-message {
  text-align: left; /* 接收方消息左对齐 */
  /* 其他样式设置 */
}
</style>
